﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MobileInterviewer.pl.studentlive.nosoifighters;

namespace MobileInterviewer
{
    public partial class NodeList : Form
    {
        private ArrayList componentList;
        private NodeDC[] nodes;
        private int nodeID;
        private int netID;

        public NodeList(int nodeID, int netID)
        {
            this.nodeID = nodeID;
            this.netID = netID;
            InitializeComponent();
            this.componentList = new ArrayList();
            showNodeList();
        }

        private NodeDC[] getNodes()
        {
            DataProviderService ws = new DataProviderService();
            return ws.GetNodes();
        }

        private void showNodeList()
        {
            nodes = getNodes();
            int i = 0;
            foreach (var n in this.nodes)
            {
                if (n.nodeID != this.nodeID)
                {
                    CheckBox checkbox = new CheckBox();
                    checkbox.Text = n.name + " " + n.surname;
                    checkbox.Name = checkbox.Text;
                    checkbox.Location = new System.Drawing.Point(3, 40 + i * 30);
                    checkbox.Size = new System.Drawing.Size(160, 30);
                    this.Controls.Add(checkbox);
                    componentList.Add(checkbox);
                    i++;
                }
            }
        }

        private void createEdges()
        {
            ArrayList alist = new ArrayList();
            int len = 0;
            foreach (var c in componentList)
            {
                CheckBox checkbox = (CheckBox)c;
                if (checkbox.Checked)
                {
                    alist.Add(nodes[componentList.IndexOf(c)]);
                    len++;
                }
            }
            EdgeDC[] edges = new EdgeDC[len];
            for (int i = 0; i < len; i++)
            {
                edges[i] = new EdgeDC();
                edges[i].leftNodeID = nodeID;
                edges[i].rightNodeID = ((NodeDC)alist[i]).nodeID;
                edges[i].netID = netID;
            }
            DataProviderService ws = new DataProviderService();
            ws.NewEdges(edges);
        }

        private void nextMenuItem_Click(object sender, EventArgs e)
        {
            createEdges();
            //Program.toMainForm();
            Program.setState(Program.mainFormState);
        }

        private void backMenuItem_Click(object sender, EventArgs e)
        {
            //Program.toStartForm();
            Program.setState(Program.helloFormState);
        }
    }
}